package com.photoeditor.demo.util;

import android.content.Context;
import android.content.res.Configuration;
import android.os.Build;
import android.os.LocaleList;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

import com.common.base.ComponentContext;
import com.common.base.log.DLog;
import com.photoeditor.demo.model.preference.SPDataManager;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Locale;

/**
 *
 * Created by wu-mingguang on 2018/7/16.
 */
public class LanguageManager {
    public static String TAG = LanguageManager.class.getSimpleName();

    public static void updateLanguage(Context context) {
        String language = SPDataManager.getLanguage();
        if (!TextUtils.isEmpty(language)) {
            updateLanguage(context, new Locale(language, getCountry(context)));
        }
    }

    private static void updateLanguage(Context context, Locale locale) {
        if (needToUpdateLocal(context, locale)) {
            Locale.setDefault(locale);
            Configuration configuration = context.getResources().getConfiguration();
            if (Build.VERSION.SDK_INT >= 24) {
                configuration.setLocales(new LocaleList(new Locale[]{locale}));
            } else if (Build.VERSION.SDK_INT >= 19) {
                configuration.setLocale(locale);
            } else {
                configuration.locale = locale;
            }
            context.getResources().updateConfiguration(configuration, context.getResources().getDisplayMetrics());
        }
    }

    private static boolean needToUpdateLocal(Context context, Locale locale) {
        return (locale == null || getLocalByConfiguration(context).equals(locale)) ? false : true;
    }

    private static Locale getLocalByConfiguration(Context context) {
        if (Build.VERSION.SDK_INT >= 24) {
            return context.getResources().getConfiguration().getLocales().get(0);
        }
        return context.getResources().getConfiguration().locale;
    }


    public static String getLanguage() {
        String language = SPDataManager.getLanguage();
        if (TextUtils.isEmpty(language)) {
            language = getLocalByConfiguration(ComponentContext.getContext()).getLanguage().toLowerCase();
        }
        DLog.d(TAG, "CurrentLanguage: " + language);
        return language;
    }

    public static String readRawResource(int rawId) {
        return inputStream2String(ComponentContext.getContext().getResources().openRawResource(rawId));
    }

    public static final int DEFAULT_BUFFER_SIZE = 4096;
    public static String inputStream2String(InputStream inputStream) {
        String str = "";
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        try {
            byte[] bArr = new byte[DEFAULT_BUFFER_SIZE];
            while (true) {
                int read = inputStream.read(bArr, 0, DEFAULT_BUFFER_SIZE);
                if (read != -1) {
                    byteArrayOutputStream.write(bArr, 0, read);
                } else {
                    String str2 = new String(byteArrayOutputStream.toByteArray());
                    try {
                        inputStream.close();
                        byteArrayOutputStream.close();
                        return str2;
                    } catch (IOException e) {
                        DLog.e(TAG, "InputStreamTOString error = " + e.toString());
                        return str2;
                    }
                }
            }
        } catch (IOException e2) {
            DLog.e(TAG,"InputStreamTOString error = " + e2.toString());
            try {
                inputStream.close();
                byteArrayOutputStream.close();
                return str;
            } catch (IOException e22) {
                DLog.e(TAG,"InputStreamTOString error = " + e22.toString());
                return str;
            }
        } catch (Throwable th) {
            try {
                inputStream.close();
                byteArrayOutputStream.close();
            } catch (IOException e3) {
                DLog.e(TAG,"InputStreamTOString error = " + e3.toString());
            }
            throw th;
        }
    }

    public static String getCountry(Context context) {
        String spCountry = SPDataManager.getCountry();
        if (!TextUtils.isEmpty(spCountry)) {
            return spCountry;
        }
        String simCountryIso;
        try {
            TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            if (telephonyManager != null) {
                simCountryIso = telephonyManager.getSimCountryIso();
                if (TextUtils.isEmpty(simCountryIso)) {
                    simCountryIso = Locale.getDefault().getCountry().toLowerCase();
                }
                return simCountryIso;
            }
        } catch (Throwable th) {
            th.printStackTrace();
        }
        simCountryIso = spCountry;
        if (TextUtils.isEmpty(simCountryIso)) {
            simCountryIso = Locale.getDefault().getCountry().toLowerCase();
        }
        return simCountryIso;
    }

}
